<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/06/02
// +----------------------------------------------------------------------

namespace app\erp\model;

use think\Model;

/**
 * 优惠券管理
 * @package app\erp\model
 */
class ErpCoupon extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'xm_erp_coupon';

    /**
     * 查询数据
     * @param id int 优惠券id
     * @author jason
     * @return array_out(code, message, (int)id)
     */
    public function getInfoById($id, $cache = true){
        $info = $this -> cache($cache) -> find($id);
        if($info){
            return array_out(200, 'success', $info);
        }else{
            return array_out(-1001, 'error');
        }
    }

    /**
     * 添加数据
     * @param coupon_name 	            string      优惠券名称
     * @param coupon_value 		        string      折扣券价值
     * @param coupon_rule 		        tinyint     发放规则(0自动;1手动)
     * @param coupon_claim_status 		string      发放状态(0未发放;1已发放;)
     * @param coupon_type 		        text        折扣券类型(1代金券)
     * @param coupon_source 	        string      来源(1线上)
     * @param coupon_period_stime 		int         有效开始时间
	 * @param coupon_period_etime 		int         有效截止时间
     * @param coupon_sum 				int         生成数量
     * @param coupon_attribute 			tinyint     属性(0不重复;1可重复)
     * @param coupon_use_condition 		tinyint     使用条件(0无条件;)
     * @param coupon_use_scope 			tinyint     使用范围(0全站;)
     * @param coupon_use_scope_info 	text        使用范围详情
     * @param coupon_isuse 			    tinyint     是否可用(0可用;1不可用)
     * @param create_time 				int         创建时间
     * @param update_time 				int         编辑时间
     * @author dou<51779987@qq.com>
     * @return array_out(code, message, (int)id)
     */
    public function addData($coupon_name, $coupon_value, $coupon_claim_status, $coupon_type, $coupon_source, $coupon_period, $coupon_period_stime, $coupon_period_etime, $coupon_sum, $coupon_attribute, $coupon_attribute_num, $coupon_use_condition, $coupon_use_condition_num, $coupon_use_scope, $coupon_use_scope_info='',$coupon_isuse){
        $data['coupon_name']                = $coupon_name;
        $data['coupon_value']               = $coupon_value;
/*        $data['coupon_rule']                = $coupon_rule;
        $data['coupon_rule_type']           = $coupon_rule_type;*/
        $data['coupon_claim_status']        = $coupon_claim_status;
        $data['coupon_type']                = $coupon_type;
        $data['coupon_source']              = $coupon_source;
        $data['coupon_period']              = $coupon_period;
        $data['coupon_period_stime']        = $coupon_period_stime;
        $data['coupon_period_etime']        = $coupon_period_etime;
        $data['coupon_sum']                 = $coupon_sum;
        $data['coupon_attribute']           = $coupon_attribute;
        $data['coupon_attribute_num']       = $coupon_attribute_num;
        $data['coupon_use_condition']       = $coupon_use_condition;
        $data['coupon_use_condition_num']   = $coupon_use_condition_num;
        $data['coupon_use_scope']           = $coupon_use_scope;
        $data['coupon_use_scope_info']      = $coupon_use_scope_info;
        $data['coupon_isuse']               = $coupon_isuse;
        $res = $this -> isUpdate(false) ->data($data, true) -> save();
        if($res){
            return array_out(200, 'success', $this->id);
        }else{
            return array_out(-1001, 'error');
        }
    }


    /**
     * 编辑数据
     * @param coupon_name 	                string      优惠券名称
     * @param coupon_value 		            string      折扣券价值
     * @param coupon_rule 		            tinyint     发放规则(0自动;1手动)
     * @param coupon_claim_status 		    string      发放状态(0未发放;1已发放;)
     * @param coupon_type 		            text        折扣券类型(1代金券)
     * @param coupon_source 	            string      来源(1线上)
     * @param coupon_period_stime 		    int         有效开始时间
     * @param coupon_period_etime 		    int         有效截止时间
     * @param coupon_sum 				    int         生成数量
     * @param coupon_attribute 			    tinyint     属性(0不重复;1可重复)
     * @param coupon_use_condition 		    tinyint     使用条件(0无条件;)
     * @param coupon_use_condition_num 		tinyint     使用条件(0无条件;)
     * @param coupon_use_scope 			    tinyint     使用范围(0全站;)
     * @param coupon_use_scope_info 	    text        使用范围详情
     * @param coupon_isuse 			        tinyint     是否可用(0可用;1不可用)
     * @param create_time 				    int         创建时间
     * @param update_time 				    int         编辑时间
     * @author dou<51779987@qq.com>
     * @return array_out(code, message, (int)id)
     */
	public function editData($id, $coupon_name, $coupon_value, $coupon_claim_status, $coupon_type, $coupon_source, $coupon_period,$coupon_period_stime, $coupon_period_etime, $coupon_sum, $coupon_attribute, $coupon_attribute_num, $coupon_use_condition,$coupon_use_condition_num,  $coupon_use_scope, $coupon_use_scope_info,$coupon_isuse){
		$map = [];
		$map['id'] = $id;
		$data = [];
        $data['coupon_name']            = $coupon_name;
        $data['coupon_value']           = $coupon_value;
       /* $data['coupon_rule']            = $coupon_rule;
        $data['coupon_rule_type']      = $coupon_rule_type;*/
        $data['coupon_claim_status']    = $coupon_claim_status;
        $data['coupon_type']            = $coupon_type;
        $data['coupon_source']          = $coupon_source;
        $data['coupon_period']          = $coupon_period;
        $data['coupon_period_stime']    = $coupon_period_stime;
        $data['coupon_period_etime']    = $coupon_period_etime;
        $data['coupon_sum']             = $coupon_sum;
        $data['coupon_attribute']       = $coupon_attribute;
        $data['coupon_attribute_num']   = $coupon_attribute_num;
        $data['coupon_use_condition']   = $coupon_use_condition;
        $data['coupon_use_condition_num']   = $coupon_use_condition_num;
        $data['coupon_use_scope']       = $coupon_use_scope;
        $data['coupon_use_scope_info']  = $coupon_use_scope_info;
        $data['coupon_isuse']           = $coupon_isuse;
        $res = $this ->update($data, $map);
		if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
	}
	
	/**
     * 删除数据
     * @param int $id
     * @author dou<51779987@qq.com>
     * @return bool
     */
    public function deleteData($id){
        $res = self::destroy($id);
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }
}
